package com.lmaos.basetools.authority;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 
 * @author lmaos
 *
 * <p>作者：张星宇</p>
 * <p>时间：2017年6月1日 下午11:10:15</p>
 * <p>说明:权限管理实现		</p>
 *
 */
public class AuthorityMgImpl implements AuthorityMg{
	protected final Map<String, UserAuthority> uauthorMap = new ConcurrentHashMap<String, UserAuthority>(128);
	protected final Map<String, UserRole> uroleMap = new ConcurrentHashMap<String, UserRole>(128);
	@Override
	public UserAuthority getUserAuthority(String userRoleKey) {
		if(userRoleKey == null){
			return null;
		}
		return uauthorMap.get(userRoleKey);
	}

	@Override
	public void setUserAuthority(String userRoleKey, UserAuthority userAuthority) {
		uauthorMap.put(userRoleKey, userAuthority);
	}
	@Override
	public void addUserAuthority(String userRoleKey, int capacity) {
		
		if(!existUserAuthority(userRoleKey)){
			setUserAuthority(userRoleKey, capacity>0?new UserAuthority(capacity):new UserAuthority());
		}
	}
	@Override
	public void addUserAuthority(String userRoleKey){
		if(!existUserAuthority(userRoleKey)){
			setUserAuthority(userRoleKey, new UserAuthority());
		}
	}

	@Override
	public UserRole getUserRole(String userRoleKey) {
		UserRole userRole = uroleMap.get(userRoleKey);
		if(userRole == null){
			userRole = new UserRole(userRoleKey, "临时角色", true); 
		}
		return userRole;
	}

	@Override
	public String getUserRoleNickname(String userRoleKey) {
		
		return getUserRole(userRoleKey).getUserRoleNickname();
	}

	@Override
	public void addUserRole(UserRole userRole) {
		String userRoleKey = userRole.getUserRoleKey();
		uroleMap.put(userRoleKey, userRole);
	}

	@Override
	public UserAuthority removeUserAuthority(String userRoleKey) {
		
		return uauthorMap.remove(userRoleKey);
	}
	@Override
	public boolean existUserAuthority(String userRoleKey){
		return uauthorMap.containsKey(userRoleKey);
	}
	@Override
	public boolean existUserRole(String userRoleKey) {
		return uroleMap.containsKey(userRoleKey);
	}
}
